<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="https://unpkg.com/@babel/standalone@7.8.7/babel.min.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react@16.8.6/umd/react.development.js"></script>
    <script crossorigin="anonymous" src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.development.js"></script>
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="text/babel" data-presets="env,stage-3,react">
      const {
        testHelpers: { createDirectLineEmulator },
        WebChat: {
          internal: { InjectStyleElements }
        }
      } = window;

      function createStyle(body) {
        const styleElement = document.createElement('style');

        const { sheet } = styleElement;

        styleElement.dataset['testid'] = 'style';
        styleElement.innerHTML = body;

        return styleElement;
      }

      run(async function () {
        const { directLine, store } = createDirectLineEmulator();

        const render = ({ nonce, styleElements, stylesRoot }) =>
          new Promise(resolve =>
            ReactDOM.render(
              <InjectStyleElements at={stylesRoot} nonce={nonce} styleElements={styleElements} />,
              document.getElementById('webchat'),
              resolve
            )
          );

        const styleElement1 = createStyle('main { background-color: Red!important; }');
        const styleElement2 = createStyle('main { background-color: Orange!important; }');
        const styleElement3 = createStyle('main { background-color: Yellow!important; }');

        await render({ styleElements: [styleElement1] });

        // THEN: Should render in red.
        await host.snapshot('local');

        const allInjectedStyles1 = document.head.querySelectorAll('style[data-testid="style"]');

        expect(allInjectedStyles1).toHaveLength(1);
        expect(allInjectedStyles1[0]).toHaveProperty('textContent', styleElement1.textContent);

        // THEN: <script> should be cloned.
        expect(allInjectedStyles1[0]).not.toBe(styleElement1);

        await render({ styleElements: [styleElement1, styleElement2] });

        // THEN: Should render in orange.
        await host.snapshot('local');

        const allInjectedStyles2 = document.head.querySelectorAll('style[data-testid="style"]');

        expect(allInjectedStyles2).toHaveLength(2);
        expect(allInjectedStyles2[0]).toHaveProperty('textContent', styleElement1.textContent);
        expect(allInjectedStyles2[1]).toHaveProperty('textContent', styleElement2.textContent);

        await render({ styleElements: [styleElement1, styleElement2, styleElement3] });

        // THEN: Should render in yellow.
        await host.snapshot('local');

        const allInjectedStyles3 = document.head.querySelectorAll('style[data-testid="style"]');

        expect(allInjectedStyles3).toHaveLength(3);
        expect(allInjectedStyles3[0]).toHaveProperty('textContent', styleElement1.textContent);
        expect(allInjectedStyles3[1]).toHaveProperty('textContent', styleElement2.textContent);
        expect(allInjectedStyles3[2]).toHaveProperty('textContent', styleElement3.textContent);
      });
    </script>
  </body>
</html>
